#!/usr/bin/env osm
# -*- encoding: utf-8 -*-

'''
Created on 2015年3月30日

@author: cuckoocs

同步文件到 smb 远程上

'''
from smb.SMBConnection import SMBConnection
import os
import re

USER_ID = "administrator"
PASS_KEY = "12345678"
LOCAL_NAME = "monstar-216" 
SERVER_NAME = "win-d0j3knivd8t" #当远程主机是 windows 时候,需要和 windows 属性设置的主机名一致
SERVER_IP = "192.168.10.228"
SHARE_ROOT = "yui_file"
LOCAL_PATH = '/Users/monstar-216/workspace/YUI/resources/yui/Assets'


reg = r'assetbundle/((android)|(ios))/[\S]+\.\w+'

changed_files = ''' yui/Assets/assetbundle/android/vesionfile.csvbak      |   2 +-
'''


def create_connection(username, password, local_name, remote_name):
    conn = SMBConnection(username, password, local_name, remote_name)
    assert conn.connect(SERVER_IP, 139)
    return conn


def upload_files(connection, share_root, files, local_path):
    
    if not connection:
        return
    
    if files is None or len(files) == 0:
        return
    
    for fn in files:
        fp = os.path.join(local_path, fn)
        f = open(fp, 'rb')
        connection.storeFile(share_root, fn, f)
        f.close()
        
    connection.close()


def get_changed_file(changed_files, restr):
    pattern = re.compile(restr, 0)
    filepath = changed_files.split(os.linesep)
    rst = []
    for path in filepath:
        match = pattern.search(path)
        if match:
            rst.append(match.group(0))
    return rst

if __name__ == '__main__':
    files = get_changed_file(changed_files, reg)
    connection = create_connection(USER_ID, PASS_KEY, LOCAL_NAME, SERVER_NAME)
    upload_files(connection, SHARE_ROOT, files, LOCAL_PATH)
    
    print 'sync success!',os.linesep,'the end!'



